package cn.mickey.cloud.controller;


import cn.mickey.cloud.entity.Comment;
import cn.mickey.cloud.entity.House;
import cn.mickey.cloud.entity.User;
import cn.mickey.cloud.service.CommentService;
import cn.mickey.cloud.service.HouseService;
import cn.mickey.cloud.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/front")
public class FrontController {
    @Resource
    private HouseService houseService;
    @Resource
    private UserService userService;
    @Resource
    private CommentService commentService;

    @RequestMapping("head")
    public String head() {
        return "front/head";
    }

    @RequestMapping("main")
    public String desk(Model model) {
        List<House> list = houseService.getAll();
        model.addAttribute("houseList", list);
        return "front/main";
    }

    @RequestMapping("find")
    public String findHouseAll(long hid, Model model) {
        House one = houseService.getOne(hid);
        List<Comment> commentList = commentService.selectCommentByHid(hid);
        model.addAttribute("commentList",commentList);
        model.addAttribute("one", one);
        return "front/home_details";
    }

    @RequestMapping("update")
    public String update(User user) {
        userService.upp(user);
        return "front/head";
    }

    @RequestMapping("one")
    public String getOne(long uid, Model model, HttpSession session) {
        User user = userService.getById(uid);
        model.addAttribute("user", user);
        session.setAttribute("user", user);
        return "front/UserUpp";
    }

    @RequestMapping("exit")
    public String exit(HttpServletRequest request) {
        request.getSession().invalidate();
        return "user/login";
    }
}

